package com.demo.service.sys.impl;

import com.demo.dao.common.entity.CatalogItem;
import com.demo.dao.sys.entity.SysPermissionInfo;
import com.demo.dao.sys.entity.SysPermissionItem;
import com.demo.dao.sys.mapper.*;
import com.demo.service.sys.SysPermissionInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysPermissionInfoServiceImpl implements SysPermissionInfoService {
    @Autowired
    private SysPermissionInfoMapper infoMapper;


    //读取权限表
    @Override
    public List<CatalogItem> getPermissionInfoByParams(Map<String, String> map) {
        return infoMapper.getPermissionInfoByParams(map);
    }

    //存储
    @Override
    public boolean savePermissionInfo(SysPermissionInfo info) {
        Long id = 0L;
        Map<String, String> map = new HashMap<>();

        SysPermissionItem r = new SysPermissionItem();
        List<CatalogItem> list = new ArrayList<>();
        r = info.getRecord();
        list = info.getList();

        //删除
        map.put("masterId", r.getMasterId().toString());
        map.put("fieldTypeId", r.getFieldTypeId().toString());
        infoMapper.removePermissionItem(map);

        //重新插入
        for (int i = 0; i < list.size(); i++) {
            r.setFieldId(list.get(i).getId());
            r.setFieldValue(list.get(i).getLabel());
            infoMapper.insertPermissionItem(r);
        }

        return true;
    }
}
